# from django.http import JsonResponse
from backend.cews.services.AssetService import create_asset_relation, soft_delete_asset_relation, update_asset_relation, import_asset_lv4_relation, import_asset_lv5_relation, import_asset_eq_relation
from drf_spectacular.utils import extend_schema
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.mixins import (CreateModelMixin,ListModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin)
from rest_framework.viewsets import GenericViewSet

from backend.cews.models import QRMap
from utils.autoschema import AutoDocSchema
from utils.decorators import request_data, error_response, check_group_permission

from .serializers import QRMapSerializer, EquipmentLevelRelationSerializer

class QRMapViewSet(
    GenericViewSet,
    RetrieveModelMixin,
    ListModelMixin,
    UpdateModelMixin,
    CreateModelMixin,
    DestroyModelMixin
):
    serializer_class = QRMapSerializer
    queryset = QRMap.objects.all()

    @extend_schema(responses={200: QRMapSerializer(many=True)})
    @action(detail=False, methods=["post"], url_name="map_qrcode",schema=AutoDocSchema())
    @request_data(keys=["qr_string"], types={"qr_string": str})
    @error_response()
    def map_qrcode(self,request,qr_string):
        '''
            desc: Get stock code and serial no by qr_string
            
            parameters:
                qr_string:
                    type: string
         
        '''

        data = self.queryset.filter(qr_string=qr_string).first()

        if not data:
            raise ValueError('QR code not found')
            return Response(status=status.HTTP_200_OK, data={
                "message": "QR code not found",
                "code":0
            })

                          
        serializer = QRMapSerializer(data)
        return Response(status=status.HTTP_200_OK, data={
            "code": 1,
            "data": serializer.data
        })
    


    @extend_schema(responses={200: QRMapSerializer(many=True)})
    @action(detail=False, methods=["post"], url_name="create_map_qrcode",schema=AutoDocSchema())
    @request_data(keys=["qr_string","stock_code","serial_no"], types={"qr_string": str,"stock_code": str,"serial_no": str})
    @error_response()
    def create_map_qrcode(self,request,qr_string,stock_code,serial_no):
        '''
            desc: Create qr mapping 
            
            parameters:
                qr_string:
                    type: string
                stock_code:
                    type: string
                serial_no:
                    type: string
         
        '''
    
        if QRMap.objects.filter( qr_string=qr_string,
            stock_code=stock_code,
            serial_no=serial_no).exists():

            raise ValueError('New QR code format already exist')


        qrmap = QRMap(
            qr_string=qr_string,
            stock_code=stock_code,
            serial_no=serial_no
        ) 

        qrmap.save()
                        
        return Response(status=status.HTTP_200_OK, data={
            "code": 1,
            "message": 'success'
        })
    
      

 
    @action(detail=False, methods=["post"], url_name="get_qr_format_list",schema=AutoDocSchema())
    @request_data(keys=["stock_code","serial_no"], types={ "stock_code": str,"serial_no": str})
    @error_response()
    def get_qr_format_list(self,request,stock_code,serial_no):
        '''
            desc: Create qr mapping 
            
            parameters:
                qr_string:
                    type: string
                stock_code:
                    type: string
                serial_no:
                    type: string
         
        '''


        if QRMap.objects.filter(stock_code=stock_code,serial_no=serial_no).exists():
            ret = []
            print(stock_code, serial_no)
            

            query_set = QRMap.objects.filter(stock_code=stock_code,serial_no=serial_no)
            for data in query_set:
                ret.append(data.qr_string)

            return Response(status=status.HTTP_200_OK, data={
                    "code": 1,
                    "message": 'success',
                    "data": ret
                })
        else:
            ret = []

            return Response(status=status.HTTP_200_OK, data={
                    "code": 0,
                    "message": 'success',
                    "data": ret
                })

                          

 